<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="1000"
    top="30px"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="80px"
      label-position="right"
    >
      <el-form-item label="文稿标题" prop="articleTitle">
        <el-input
          v-model="form.articleTitle"
          placeholder="请输入文稿标题"
          maxlength="50"
          show-word-limit
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item label="中文摘要" prop="abstractZn">
        <el-input
          type="textarea"
          rows="5"
          v-model="form.abstractZn"
          placeholder="请输入中文摘要"
          maxlength="500"
          show-word-limit
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="英文摘要" prop="abstractEn">
        <el-input
          type="textarea"
          rows="5"
          v-model="form.abstractEn"
          placeholder="请输入英文摘要"
          maxlength="500"
          show-word-limit
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="form.keyword"
          placeholder="请输入关键字"
          maxlength="250"
          show-word-limit
          clearable
          type="textarea"
        ></el-input>
      </el-form-item>
      <el-form-item label="属于领域" prop="fieldId">
        <el-select
          v-model="form.fieldId"
          class="m-2"
          placeholder="请选择属于领域"
          style="width: 20%"
        >
          <el-option
            v-for="item in fieldDic"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="属于栏目" prop="columnId">
        <el-select
          v-model="form.columnId"
          class="m-2"
          placeholder="请选择属于栏目"
          style="width: 20%"
        >
          <el-option
            v-for="item in columnDic"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="文稿上传" prop="articleFile" v-if="mode != 'show'">
        <sc-upload-file
          v-model="form.downloadFile"
          :limit="1"
          tip="单个文件不要超过10M,请上传doc/docx/pdf格式文件"
          accept=".doc,.docx,.pdf"
          :onSuccess="onSuccess"
        >
          <el-button type="primary" icon="el-icon-upload">上传附件</el-button>
        </sc-upload-file>
      </el-form-item>
      <el-form-item label="温馨提示" prop="articleFile" v-else>
        <sc-upload-file v-model="form.downloadFile" :limit="1">
          <span style="margin-left: 8px; color: #909399; font-size: 14px"
            >点击下方文件，下载文稿附件</span
          >
        </sc-upload-file>
      </el-form-item>
    </el-form>
    <template #footer>
      <div v-if="form.checkStatus == 0">
        <el-button type="success" :loading="isSaveing" @click="success()"
          >通 过</el-button
        >
        <el-button type="danger" :loading="isSaveing" @click="openReject()"
          >驳 回</el-button
        >
      </div>
    </template>
    <el-dialog
      v-model="dialog"
      :size="560"
      title="驳回"
      direction="rtl"
      destroy-on-close
    >
      <el-form
        :model="form2"
        :rules="rules2"
        label-width="80px"
        label-position="right"
        ref="dialogForm2"
      >
        <el-form-item label="修改建议" prop="callbackAdvise">
          <el-input
            type="textarea"
            rows="5"
            v-model="form2.callbackAdvise"
            placeholder="请输入驳回理由或者修改建议"
            maxlength="500"
            show-word-limit
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="success" :loading="isSaveing" @click="reject()"
            >提 交</el-button
          >
          <el-button @click="dialog = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </el-dialog>
</template>

<script>
export default {
  emits: ["success", "closed"],
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增",
        edit: "编辑",
        show: "审核",
      },
      dialog: false,
      visible: false,
      isSaveing: false,
      status: 1,
      //表单数据
      form: {
        id: "",
        articleTitle: "",
        articleFile: "",
        abstractZn: "",
        abstractEn: "",
        keyword: "",
        fieldId: null,
        columnId: null,
      },
      form2: {
        callbackAdvise: "",
      },
      //验证规则
      rules: {
        articleTitle: [{ required: true, message: "文稿标题不能为空" }],
        abstractZn: [{ required: true, message: "中文摘要不能为空" }],
        abstractEn: [{ required: true, message: "英文摘要不能为空" }],
        keyword: [{ required: true, message: "关键字不能为空" }],
        fieldId: [{ required: true, message: "所属领域不能为空" }],
        columnId: [{ required: true, message: "所属栏目不能为空" }],
        articleFile: [{ required: true, message: "所属栏目不能为空" }],
      },
      rules2: {
        callbackAdvise: [{ required: true, message: "文稿标题不能为空" }],
      },
      columnDic: [],
      fieldDic: [],
    };
  },
  mounted() {
    this.getColumnDic();
    this.getFieldDic();
  },
  methods: {
    //栏目字典列表
    async getColumnDic() {
      var columnDic = await this.$API.column.dicList.get();
      this.columnDic = columnDic.data;
    },
    //栏目字典列表
    async getFieldDic() {
      var fieldDic = await this.$API.field.dicList.get();
      this.fieldDic = fieldDic.data;
    },
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    onSuccess(res) {
      console.log(res);
      if (res.code == "200") {
        this.form.articleFile = res.data.fileName;
      } else {
        this.$message.error("上传失败");
      }
      console.log(this.form);
    },
    //通过审核
    async success() {
      console.log("success");
      var reqData = new FormData();
      reqData.append("id", this.form.id);
      reqData.append("articleStatus", 2);
      var res = await this.$API.article.check.post(reqData);
      if (res.code == "200") {
        this.$emit("success", this.form, "add");
        this.$message.success("审核完成");
        this.visible = false;
      } else {
        this.$alert(res.message, "提示", { type: "error" });
      }
    },
    openReject() {
      this.dialog = true;
    },
    //驳回文稿
    async reject() {
      this.$refs.dialogForm2.validate(async (valid) => {
        if (valid) {
          var reqData = new FormData();
          reqData.append("id", this.form.id);
          reqData.append("articleStatus", 3);
          reqData.append("callbackAdvise", this.form2.callbackAdvise);
          var res = await this.$API.article.check.post(reqData);
          if (res.code == "200") {
            this.$emit("success", this.form, "add");
            this.dialog = false;
            this.visible = false;
            this.$message.success("审核完成");
          } else {
            this.$alert(res.message, "提示", { type: "error" });
          }
        } else {
          return false;
        }
      });
    },
    //表单注入数据
    setData(row) {
      Object.assign(this.form, row);
    },
  },
};
</script>

<style>
</style>
